﻿function Widget_RSS(obj)
{
   this.initializeBase(obj);
}
Widget_RSS.prototype = new WidgetInstance;

Widget_RSS.prototype.Save = function()
{
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      this.FeedCount = this.EditSelect.value;
      var val = this.FeedCount + "," + this.FeedURL;
      
      JSON.Widget_UpdateProperty(this.Widget.Tab.id, this.id, "state", val);
      this.Refresh();
   }
}

Widget_RSS.prototype.Refresh = function()
{
   WriteHistory("Widget_RSS.prototype.Refresh #" + this.id);
   //Query proxyasync for the rss feed
   Proxy.GetRss(this.FeedURL,parseInt(this.FeedCount),10,Function.createDelegate( this, this.onContentLoad ));
}

Widget_RSS.prototype.onContentLoad = function(rss)
{
   WriteHistory("Widget_RSS.prototype.onContentLoad #" + this.id);
     var div = this.WidgetDiv_Body;
     div.innerHTML = "";
     
     if (!rss)
     {
        div.innerHTML = "we are unable to grab the rss file";
        return;
     }
     else if (rss.length < 1)
     {
      div.innerHTML = "No news was returned";
     }

     // creates a <table> element and a <tbody> element
     
     //table element
     var tbl     = document.createElement("table");
     tbl.className="rss_tbl";
     
     //table body element
     var tblBody = document.createElement("tbody");
     
     //replace string for regular expression
     var replacestr = "\\'";

     //gets the element width
     var eWidth = getElementWidth(div);
     
     //assuming each character has a max width of 5
     var HeaderDesc = eWidth / 5;
     var BulletDesc = HeaderDesc - 3;
     
     var tmp = "";
     
     for( var i = 0; i < rss.length; i ++ )
     {
         var item = rss[i];
         
         var HintWidth = 200;
            
         if (i==0)
         {
            if (item.ContentEncoded.length > 0)
            {
               //if there is Content, use the content, else use the description
               item.ODesc = "";
               item.Description = "";
            }
            else
            {
               if (item.Description.length > HeaderDesc)
               {
                  item.ODesc = item.Description.replace(/'/g, replacestr).replace(/"/g, replacestr);
                  item.Description = item.Description.substring(0, HeaderDesc) + "..<img src='/widgets/generic/rss/rss_desc_link.gif' border='0' />";
               }
            }
            //First display as text
            var span = document.createElement("span");
            
            span.innerHTML = "<a class='feed_item_link_first_title'" +
                  (item.Link.length > 0 ? " target='_blank' href='" + item.Link + "'" : " href='javascript:void(0)'") +
                  (item.ContentEncoded.length < 1 ? " onmouseover=\"Tip('" + item.ODesc + "', BALLOON, true, WIDTH, " + HintWidth + ");\" onmouseout='UnTip()'" : "") +
                  ">" + item.Title + 
                  (item.ContentEncoded.length < 1 ? "<span class='feed_item_link'>" + item.Description + "</span>": item.ContentEncoded) +
                  "</a>";
            
            div.appendChild(span);
         }
         else
         {
            //2nd onwards create as bullets
            if (item.Title.length > BulletDesc)
            {
               item.Title = item.Title.substring(0, BulletDesc) + "..";
            }
            
            if (item.ContentEncoded.length > 0)
            {
               //if there is Content, use the content, else use the description
               item.ODesc = "";
               item.Description = "";
            }
            else
            {
               item.ODesc = item.Description.replace(/'/g, replacestr).replace(/"/g, replacestr);
            }            

            var row = document.createElement("tr");
            row.setAttribute("valign", "top");
            var cell1 = document.createElement("td");
            var ElementID = this.id + "_" + i;
            var ToggleFunction = "$WI(" + this.id + ").ToggleMessage('" + ElementID + "')";
            cell1.innerHTML = "<div onclick=\"" + ToggleFunction + "\" id='wrss_t" + ElementID + "' class='plus'></div>";

            var cell2 = document.createElement("td");

            cell2.innerHTML = "<a class='feed_item_link'" +
                  (item.Link.length > 0 ? " target='_blank' href='" + item.Link + "'" : " href='javascript:void(0)'") +
                  (item.ContentEncoded.length < 1 ? " onmouseover=\"Tip('" + item.ODesc + "', BALLOON, true, WIDTH, " + HintWidth + ");\" onmouseout='UnTip()'" : "") +
                  ">" + item.Title + 
                  "<div id='wrss_m" + ElementID + "' style='display:none'>" +
                  (item.ContentEncoded.length < 1 ? "<span class='feed_item_link'>" + item.Description + "</span>": item.ContentEncoded) +
                  "</div></a>";
            
            row.appendChild(cell1);
            row.appendChild(cell2);
            tblBody.appendChild(row);
         }
     }
     tbl.appendChild(tblBody);
     div.appendChild(tbl);   
}

Widget_RSS.prototype.Load = function()
{
   this.Menu_Edit = new Array();
   this.Menu_Edit.push("m" + this.id + "_edit");
   this.Widget.InstanceLoad();
      
   this.EditSelect = document.getElementById("wddl" + this.Widget.id);
   
   var theState = this.Widget.obj.State;
   this.FeedCount = parseInt(theState.substring(0, theState.indexOf(",")));
   
   if (!this.FeedCount || isNaN(this.FeedCount))
   {
      this.FeedCount = 0;
   }
   this.FeedURL = theState.substring(theState.indexOf(",")+1);
   
   this.EditSelect.value = this.FeedCount;
   
   this.Refresh();
   
   this.Widget.InstanceLoadComplete();
}

Widget_RSS.prototype.ToggleMessage = function(ElementID)
{
   var ToggleElement = $get('wrss_t' + ElementID);
   var DivElement = $get('wrss_m' + ElementID);
   
   if (ToggleElement.className.toLowerCase() == 'minus')
   {
      ToggleElement.className = "plus";
      DivElement.style.display = "none";
   }
   else
   {
      ToggleElement.className = "minus";
      DivElement.style.display = "block";
   }
}